"use client"; import Breadcrumb from "@/app/components/reuseableUI/breadcrumb"; import EmptyState from "@/app/components/reuseableUI/emptyState"; import { ProductCard } from "@/app/components/reuseableUI/productCard"; import ItemsPerPageSelectClient from "@/app/components/shop/ItemsPerPageSelectClient"; import SearchFilterClient from "@/app/components/shop/SearchFilterClient"; import type { PLSearchProduct } from "@/lib/client/partslogic"; import { partsLogicClient } from "@/lib/client/partslogic"; import { useEffect, useState } from "react"; type ItemsPerPage = 10 | 20 | 50 | 100; interface PaginationInfo { total: number; page: number; per_page: number; total_pages: number; } interface BrandPageClientProps { brandSlug: string; brandName: string; initialProducts?: PLSearchProduct[] | null; initialPagination?: PaginationInfo | null; } export default function BrandPageClient(props: BrandPageClientProps) { const { brandSlug, brandName, initialProducts, initialPagination } = props; const hasInitialData = initialProducts && initialProducts.length > 0; const [itemsPerPage, setItemsPerPage] = useState(10); const [searchQuery, setSearchQuery] = useState(""); const [products, setProducts] = useState( initialProducts || [] ); const [loading, setLoading] = useState(!hasInitialData); const [loadingMore, setLoadingMore] = useState(false); const [categoryName, setCategoryName] = useState(""); const [pagination, setPagination] = useState({ total: initialPagination?.total || 0, page: initialPagination?.page || 1, per_page: initialPagination?.per_page || 10, total_pages: initialPagination?.total_pages || 0, }); const [isInitialized, setIsInitialized] = useState(hasInitialData); useEffect(() => { // Skip initial fetch if we have SSR data and haven't changed filters if ( isInitialized && hasInitialData && itemsPerPage === (initialPagination?.per_page || 10) && !searchQuery ) { return; } const fetchProducts = async () => { setLoading(true); try { const response = await partsLogicClient.searchProducts({ brand_slug: brandSlug, page: 1, per_page: itemsPerPage, q: searchQuery || undefined, }); setProducts(response.products || []); setPagination(response.pagination); if (response.products && response.products.length > 0) { setCategoryName(response.products[0].category_name || brandName); } else { setCategoryName(brandName); } } catch (error) { console.error("Error fetching products:", error); setProducts([]); } finally { setLoading(false); setIsInitialized(true); } }; fetchProducts(); }, [ itemsPerPage, searchQuery, brandSlug, brandName, isInitialized, hasInitialData, initialPagination?.per_page, ]); const loadMore = async () => { if (loadingMore || pagination.page >= pagination.total_pages) return; setLoadingMore(true); const nextPage = pagination.page + 1; try { const response = await partsLogicClient.searchProducts({ brand_slug: brandSlug, page: nextPage, per_page: itemsPerPage, q: searchQuery || undefined, }); const newProducts = response.products || []; setProducts((prev) => [...prev, ...newProducts]); setPagination(response.pagination); } catch (error) { console.error("Error loading more products:", error); } finally { setLoadingMore(false); } }; const Title = brandName.toUpperCase(); const breadcrumbItems = [ { text: "HOME", link: "/" }, { text: "SHOP", link: "/products/all" }, { text: Title }, ]; return (
{/* H1 is rendered server-side in `src/app/brand/[id]/page.tsx` for SEO. */}

{Title}

{searchQuery && (
{pagination.total > 0 ? `Found ${pagination.total} result${ pagination.total === 1 ? "" : "s" } for "${searchQuery}" in ${categoryName || Title}` : `No results found for "${searchQuery}" in ${ categoryName || Title }`}
)} {!searchQuery && (
{pagination.total} product{pagination.total === 1 ? "" : "s"}{" "} in {categoryName || Title}
)}
{/* */}
{loading && (
)}
{products && products.length > 0 ? products .sort((a, b) => a.name.localeCompare(b.name)) .map((item) => ( item.price_min ? item.price_max - item.price_min : null } isFeatured={ item.collection_names?.includes("Best Sellers") || false } onSale={(item.price_max || 0) > (item.price_min || 0)} skus={item.skus || []} /> )) : !loading && ( )}
{!loading && pagination.page < pagination.total_pages && products.length > 0 && (
)}
); }